package com.xishu.service.print.kitchen;

import com.xishu.entity.order.Order;
import com.xishu.entity.order.OrderItem;
import com.xishu.entity.shop.Item;
import com.xishu.entity.shop.SetMenuOneItem;
import com.xishu.service.ItemService;
import com.xishu.util.ClassUtil;
import com.xishu.util.Tools;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 将订单里面的每一个菜都拆分出来
 * 如果是套餐按照标签打印则不用拆分
 */
public class SplitOrderItemCmd implements Command, Tools {
    private static Logger logger = LoggerFactory.getLogger(SplitOrderItemCmd.class);
    private Order order;


    public SplitOrderItemCmd(Order order) {
        this.order = order;
    }

    @Override
    public boolean execute(Context context) throws Exception {
        List<OrderItem> orderItemDetailList = getList(order.getOrderItemDetailList()).stream().filter(p -> getBoolean(p.getSetMenu())).collect(Collectors.toList());
        if (isEmpty(orderItemDetailList)) {
            logger.info("order item detail list is empty");
            return false;
        }

        List<OrderItem> orderItemList = new ArrayList<>();

        for (OrderItem orderItem : orderItemDetailList) {
            //如果是套餐标签打印，则不用拆分
            Long itemId = orderItem.getItemId();
            if (getLong(itemId) == 0l) {
                continue;
            }

            Item itemInDb = ItemService.getInstance().findItem(itemId);
            if (getBoolean(itemInDb.getSetMenuLabelEnable())) {
                logger.info("set menu label enable {}", itemId);
                orderItemList.add(orderItem);
                continue;
            }

            List<SetMenuOneItem> setMenuOneItemList = getList(orderItem.getSetMenuOneItemList());
            for (SetMenuOneItem setMenuOneItem : setMenuOneItemList) {
                try {
                    List<Item> itemList = setMenuOneItem.getItemList();
                    for (Item item : itemList) {
                        if (getLong(item.getId()) == 0l) {
                            logger.info("item id is 0");
                            continue;
                        }

                        OrderItem cloneOrderItem = (OrderItem) ClassUtil.clone(orderItem);
                        SetMenuOneItem cloneSetMenuOneItem = (SetMenuOneItem) ClassUtil.clone(setMenuOneItem);
                        cloneOrderItem.setSetMenuOneItemList(Arrays.asList(cloneSetMenuOneItem));
                        cloneSetMenuOneItem.setItemList(Arrays.asList(item));
                        orderItemList.add(cloneOrderItem);
                    }
                } catch (Exception e) {
                    logger.error("e", e);
                }
            }
        }

        //再加上普通规格菜
        List<OrderItem> commonItemList = getList(order.getOrderItemDetailList()).stream().filter(p -> !getBoolean(p.getSetMenu())).collect(Collectors.toList());
        if (isNotEmpty(commonItemList)) {
            orderItemList.addAll(commonItemList);
        }

        order.setOrderItemDetailList(orderItemList);
        return false;
    }
}
